package com.zhang.controller;

import com.github.pagehelper.PageInfo;
import com.zhang.pojo.ClassBean;
import com.zhang.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("class")
public class ClassController {

    @Autowired
    private IClassService classService;

    @RequestMapping("findAll")
    public String findAll(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, HttpServletRequest req){
        PageInfo<ClassBean> classes = classService.findAll(pageNo);
        System.out.println(classes);
        req.setAttribute("class",classes);
        return "/html/class/index.html";
    }

    @RequestMapping("add")
    public String add(ClassBean classBean){
        classService.add(classBean);
        return "redirect:/class/findAll";
    }

    @RequestMapping("del")
    public String del(int id){
        classService.del(id);
        return "redirect:/class/findAll";
    }

    @RequestMapping("toUpdate")
    public String toUpadte(int id, Model model){
        ClassBean classBean = classService.findById(id);
        model.addAttribute("classBean",classBean);
        return "/html/class/toUpdate.html";
    }

    @RequestMapping("update")
    public String update(int id,String teacher){
        classService.update(id,teacher);
        return "redirect:/class/findAll";
    }
}
